package com.example.esp8266.Main;

import androidx.activity.OnBackPressedCallback;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.esp8266.Main.Main_Fragment.Connect_Fragment;
import com.example.esp8266.Main.Main_Fragment.ExpandFragment;
import com.example.esp8266.Main.Main_Fragment.Home_Fragment;
import com.example.esp8266.Main.Main_Fragment.User_Fragment;
import com.example.esp8266.R;


public class MainActivity extends AppCompatActivity
        implements View.OnClickListener{

    private long backPressedTime = 0;
    private TextView centeredTitle,connect_text,device_text,expand_page_text,user_page_text;
    private ImageButton device_btn,connect_device_btn,expand_btn,user_btn;
    private boolean loginState;
    private int deviceCounts;
    private String userLevel,user_name_sql;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        centeredTitle = findViewById(R.id.centeredTitle);
        centeredTitle.setText("设备");//标题栏设置

        connect_text = findViewById(R.id.connect_device_text);
        device_text = findViewById(R.id.device_text);
        expand_page_text = findViewById(R.id.expand_text);
        user_page_text = findViewById(R.id.user_text);
        //底部四按钮文字

        device_btn = findViewById(R.id.device_button);
        connect_device_btn = findViewById(R.id.find_device_button);
        expand_btn = findViewById(R.id.expand_button);
        user_btn =  findViewById(R.id.user_button);//底部四按钮

        ImageButton menu_btn =  findViewById(R.id.menu_button);//右上菜单"+"号

        device_btn.setOnClickListener(this);//为一个按钮定义监听单击事件
        connect_device_btn.setOnClickListener(this);//为一个按钮定义监听单击事件
        expand_btn.setOnClickListener(this);//为一个按钮定义监听单击事件
        user_btn.setOnClickListener(this);//为一个按钮定义监听单击事件
        menu_btn.setOnClickListener(this);//为一个按钮定义监听单击事件

        // 获取 SharedPreferences 对象，如果文件已存在则不创建新文件
        SharedPreferences sharedPref = getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
        // 获取各参数
        loginState = sharedPref.getBoolean("login_state", false);
        deviceCounts = sharedPref.getInt("device_counts", 0);
        userLevel = sharedPref.getString("user_level", "访客模式");
        user_name_sql = sharedPref.getString("user_name", "  用户名");

        Home_Fragment mainFragment = new Home_Fragment();
        // 创建 Bundle 对象，用于存储参数args
        Bundle args = new Bundle();
        args.putInt("device_counts", deviceCounts);//格式:key+内容
        args.putString("user_name_sql", user_name_sql);
        // 将 Bundle 对象设置为 Fragment 的参数
        mainFragment.setArguments(args);
        // 添加并初始化 Fragment 到指定的 View
        getSupportFragmentManager().beginTransaction()
                .add(R.id.main_view, mainFragment)
                .commit();

        OnBackPressedCallback callback = new OnBackPressedCallback(true /* enabled by default */) {
            @Override
            public void handleOnBackPressed() {
                long currentTime = System.currentTimeMillis();
                if (currentTime - backPressedTime > 1500) {
                    // 如果时间间隔超过1.5秒，则提示双击退出
                    Toast.makeText(MainActivity.this, "再按一次退出应用", Toast.LENGTH_SHORT).show();
                    backPressedTime = currentTime;
                } else {
                    // 如果时间间隔不超过1.5秒，则执行退出Activity的逻辑
                    finish();
                }
            }
        };
        getOnBackPressedDispatcher().addCallback(this, callback);

        // 以上为初始化
    }

    @Override//调用OnClickListener接口处理各种单击事件
    public void onClick(View v) {
        if (v.getId() == R.id.device_button) {
            centeredTitle.setText("设备");

            down_menu_display_change();
            device_text.setTextColor(Color.parseColor("#f12057"));//更换菜单字体颜色

            // 更改为另一张图片
            Drawable device_on = ContextCompat.getDrawable(this, R.drawable.chip_on);
            device_btn.setImageDrawable(device_on);

            Bundle args = new Bundle();
            args.putInt("device_counts", deviceCounts);//格式:key+内容
            args.putString("user_name_sql", user_name_sql);
            Home_Fragment mainFragment = new Home_Fragment();
            // 将 args 对象设置为 Fragment 的参数
            mainFragment.setArguments(args);
            replaceFragment(mainFragment);
        }
        else if(v.getId() == R.id.find_device_button){
            centeredTitle.setText("连接");

            down_menu_display_change();
            connect_text.setTextColor(Color.parseColor("#f12057"));

            // 更改为另一张图片
            Drawable connect_on = ContextCompat.getDrawable(this, R.drawable.connect_on);
            connect_device_btn.setImageDrawable(connect_on);

            replaceFragment(new Connect_Fragment());//切连接界面
        }
        else if (v.getId() == R.id.user_button) {
            centeredTitle.setText("用户");

            down_menu_display_change();
            user_page_text.setTextColor(Color.parseColor("#f12057"));

            // 更改为另一张图片
            Drawable user_page_on = ContextCompat.getDrawable(this, R.drawable.user_on);
            user_btn.setImageDrawable(user_page_on);

            // 传递数据给 user_Fragment
            Bundle bundle = new Bundle();
            bundle.putBoolean("login_state", loginState);//格式:key+内容
            bundle.putInt("device_counts", deviceCounts);
            bundle.putString("user_level", userLevel);
            bundle.putString("user_name_sql", user_name_sql);

            User_Fragment fragment = new User_Fragment();
            fragment.setArguments(bundle);
            replaceFragment(fragment);//切用户界面
        }else if (v.getId() == R.id.expand_button){
            centeredTitle.setText("扩展");

            down_menu_display_change();
            expand_page_text.setTextColor(Color.parseColor("#f12057"));

            // 更改为另一张图片
            Drawable connect_on = ContextCompat.getDrawable(this, R.drawable.expand_btn_on);
            expand_btn.setImageDrawable(connect_on);

            replaceFragment(new ExpandFragment());//切连接界面
        }
    }

    //动态切换视图
    private void replaceFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.setCustomAnimations(R.anim.slide_left, R.anim.slide_right);
        transaction.replace(R.id.main_view,fragment);
        transaction.commit();
    }

    //底部菜单初始化
    private void down_menu_display_change()
    {
        user_page_text.setTextColor(Color.parseColor("#66ffffff"));
        device_text.setTextColor(Color.parseColor("#66ffffff"));//更换菜单字体颜色
        expand_page_text.setTextColor(Color.parseColor("#66ffffff"));//更换菜单字体颜色
        connect_text.setTextColor(Color.parseColor("#66ffffff"));//更换菜单字体颜色

        // 更改为另一张图片
        Drawable connect_off = ContextCompat.getDrawable(this, R.drawable.connect_off);
        Drawable chip_off = ContextCompat.getDrawable(this, R.drawable.chip_off);
        Drawable expand_off = ContextCompat.getDrawable(this, R.drawable.expand_btn);
        Drawable user_page_off = ContextCompat.getDrawable(this, R.drawable.user_off);
        device_btn.setImageDrawable(chip_off);
        connect_device_btn.setImageDrawable(connect_off);
        expand_btn.setImageDrawable(expand_off);
        user_btn.setImageDrawable(user_page_off);
        ;
    }

}